import { $ } from './library/jquery-module.js';
import { cookie } from './library/cookie.js';
import './addcookie.js';

let id = cookie.get('id');
let uname, birthday, phone, password;

let p = new Promise(resolve => {
    $.ajax({
        type: "get",
        url: "../../interface/getUser.php",
        data: { id },
        dataType: "json",
        success: function(res) {
            resolve(res);
        }
    });
});

p.then(val => {
    uname = val[0].uname ? val[0].uname : '您没有输入昵称';
    birthday = val[0].birthday ? val[0].birthday : '您没有输入生日';
    phone = val[0].phone.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2');
    password = getpassword(val[0].password);
    // console.log(phone);
    $('#id').text(val[0].id);
    $('#uname').text(uname);
    $('#birthday').text(birthday);
    $('#phone').text(phone);
    $('#password').text(password);
});

$('#idcard').on({
    'click': function() {
        $('#myModal').modal('show');
        $('#after').val('');
        $('#after').prop('type', 'text');
        switch ($(this).parent().prev().attr('id')) {
            case 'uname':
                $('#myModalLabel').text('修改昵称');
                $('#after').prop('name', 'uname');
                break;
            case 'birthday':
                $('#myModalLabel').text('修改生日');
                $('#after').prop('name', 'birthday');
                break;
            case 'phone':
                $('#myModalLabel').text('修改手机号码');
                $('#after').prop('name', 'phone');
                break;
            case 'password':
                $('#myModalLabel').text('修改密码');
                $('#after').prop('name', 'password');
                $('#after').prop('type', 'password');
                $('#after').val('');
                break;
        }
    }

}, '.btn');

$('#after').on({
    'keydown': function(e) {
        if (e.keyCode == 13) {
            $('#submit').trigger('click');
        }
    }
})


// UPDATE users set uname = '李四' WHERE id = '10000009';
$('#submit').on({
    'click': change
});

function change() {
    let str = '',
        id;
    str = `${$('#after').prop('name')}="${$('#after').val()}"`;
    id = cookie.get('id');
    $.ajax({
        type: "get",
        url: "../../interface/updataUser.php",
        data: {
            str,
            id
        }
    });
    switch ($('#after').prop('name')) {
        case 'password':
            location.href = "./loading.html?type=5";
            cookie.remove('id');
            cookie.remove('isLogined');
            break;
        default:
            location.reload();
    }
}


function getpassword(pwd) {
    var lvl = 0; //默认等级是0
    //判断密码中是否有数字、字母、特殊符号
    let str = '';
    // 判断数字
    if (/[0-9]/.test(pwd)) {
        lvl++;
    }
    // 判断有没有字母
    if (/[a-zA-Z]/.test(pwd)) {
        lvl++;
    }
    // 判断有没有特殊符号
    if (/[^0-9a-zA-A_]/.test(pwd)) {
        lvl++;
    }
    switch (lvl) {
        case 1:
            str = '密码强度：弱';
            break;
        case 2:
            str = '密码强度：中';
            break;
        case 3:
            str = '密码强度：强';
            break;
    }
    return str;
}